package com.framework.seedkit.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownLoadUtil {
	static File file;
	
	public static File downFile(final String httpUrl, final Handler handler) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(httpUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    FileOutputStream fileOutputStream = null;
                    InputStream inputStream;
              
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        inputStream = connection.getInputStream();
                        int totalSize = connection.getContentLength();
                        
                        if (inputStream != null) {
                            file = getFile(httpUrl);
                            
                            fileOutputStream = new FileOutputStream(file);
                            byte[] buffer = new byte[1024];
                            int length = 0;
 
                            while ((length = inputStream.read(buffer)) != -1) {
                                fileOutputStream.write(buffer, 0, length);
                                Message message = handler.obtainMessage();
        	                    message.what = 1;
        	                    handler.sendMessage(message);
//        	                    Log.e("", "-------------- length = " + length);
                            }
                            fileOutputStream.flush();
                            fileOutputStream.close();
                        }
                        inputStream.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return file;
    }
    

  public static File getFile(String url) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), getFilePath(url));
        if (file.exists()) {
        	file.delete();
        } 
        try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
        return file;
    }

  private static String getFilePath(String url) {
        return url.substring(url.lastIndexOf("/"), url.length());
    }
  
  public static void installApk(Context context, File file) {
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
      context.startActivity(intent);
  }
}
